namespace umbDashboard
{
	using System;
	using System.Data;
	using System.Drawing;
	using System.Web;
	using System.Web.UI.WebControls;
	using System.Web.UI.HtmlControls;

	using Microsoft.ApplicationBlocks.Data;
	using System.Data.SqlClient;

	/// <summary>
	///		Summary description for memberSearch.
	/// </summary>
	public partial class memberSearch : System.Web.UI.UserControl
	{

		protected void Page_Load(object sender, System.EventArgs e)
		{
			ButtonSearch.Text = umbraco.ui.Text("search");
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{

		}
		#endregion

		protected void ButtonSearch_Click(object sender, System.EventArgs e)
		{
			string sql = "select distinct '<a href=\"members/EditMember.aspx?id=' + convert(nvarchar(10), nodeId) + '\">' +  text + '</a>' as Name, loginName, email from cmsMember inner join umbracoNode on umbracoNode.id = nodeId where text like @query or email like @query or loginName like @query";

			if (CheckBoxExtended.Checked) 
			{
				sql += " or nodeId in (select nodeId from cmsPropertyData inner join cmsMember on nodeId = contentNodeid where dataNvarchar like @query or dataNtext like @query)";
			}
			sql += " order by '<a href=\"members/EditMember.aspx?id=' + convert(nvarchar(10), nodeId) + '\">' +  text + '</a>'";

			SqlDataReader dr = 
				SqlHelper.ExecuteReader(umbraco.GlobalSettings.DbDSN, 
				CommandType.Text,
				sql, new SqlParameter("@query", "%" + TextBox1.Text + "%"));
			DataGrid1.DataSource = dr;
			DataGrid1.DataBind();
			DataGrid1.Visible = true;
			dr.Close();
		}
	}
}
